package com.ac.scum.log.web.model.menu.controller;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.result.Result;
import com.ac.scum.log.bean.Menu;
import com.ac.scum.log.web.interceptor.anno.PermissionCheck;
import com.ac.scum.log.web.model.menu.service.MenuService;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理
 */
@RestController
@RequestMapping("menu")
@AllArgsConstructor
public class MenuController {

    private final MenuService menuService;

    @PermissionCheck(permission = "menu")
    @GetMapping("page/{current}/{size}")
    public Result<Page<Menu>> getMenuListPage(@PathVariable("current") Integer current, @PathVariable("size") Integer size) {
        return Result.ok(menuService.page(new Page<>(current, size), Wrappers.lambdaQuery(Menu.class).orderByAsc(Menu::getSortId)));
    }


    @GetMapping("list")
    @PermissionCheck(permission = "menu")
    public Result<List<Menu>> getMenuListPage() {
        return Result.ok(menuService.list(Wrappers.lambdaQuery(Menu.class).orderByAsc(Menu::getSortId)));
    }

    @GetMapping("server/list")
    public Result<List<Menu>> getMenus() throws BaseException {
        return Result.ok(menuService.findMenuByServerId(CurrentUserUtil.getCurrentServerId()));
    }

    @PutMapping
    @PermissionCheck(permission = "menu")
    public Result<Menu> modify(@RequestBody Menu menu) {
        menuService.updateById(menu);
        return Result.ok(menu);
    }

    @PostMapping
    @PermissionCheck(permission = "menu")
    public Result<Menu> insert(@RequestBody Menu menu) {
        menuService.save(menu);
        return Result.ok(menu);
    }

    @DeleteMapping("{id}")
    @PermissionCheck(permission = "menu")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.ok(menuService.removeById(id));
    }


}
